package com.wangc.report.controller;

import com.wangc.report.entity.LoadEntity;
import com.wangc.report.service.LoadService;
import com.wangc.report.service.TaskLogService;
import com.wangc.report.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("report/load")
public class loadController {
    private static final Logger LOG = LoggerFactory.getLogger(loadController.class);
    @Autowired
    private ReportCheck reportCheck;
    @Autowired
    private TaskLogService taskLogService;
    @Autowired
    private LoadService loadService;

    //http://127.0.0.1:8080/load/loadSourceFile?table=test&dataDt=20200901&newOldFlg=1
    @RequestMapping("/run")
    public R loadSourceFile(@RequestParam Map<String, Object> params) {
        LOG.debug(params.get("tableId").toString());
        String[] tableList = params.get("tableId").toString().split(",");
        LOG.debug(params.get("tableId").toString());
        if (tableList.length <= 0) {
            throw new RRException("输入表名为空请检查！");
        }
        if (null == params.get("dataDt")) {
            throw new RRException("输入数据日期为空请检查！");
        }
        String newOldFlg = params.get("newOldFlg").toString();
        LOG.debug(newOldFlg);
        for (String tableId : tableList) {
            loadService.loadSourceFile(tableId, params.get("dataDt").toString(), newOldFlg);
        }
        return R.ok().put("loadSourceFileState", "starting");
    }

    @RequestMapping("/loadSourceTable")
    public R loadSourceTable(@RequestParam Map<String, Object> params) {
        new Thread(() -> {
            try {
                reportCheck.loadSourceTable(params.get("table").toString(), params.get("dataDt").toString(), params.get("newOldFlg").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
        return R.ok().put("loadSourceTable", "starting");
    }

    @RequestMapping("/loadSourceFileList")
    public R loadSourceFileList(@RequestParam Map<String, Object> params) {
        List<String> tableList = (List<String>) params.get("tableList");
        String dataDt = params.get("dataDt").toString();
        String newOldFlg = params.get("newOldFlg").toString();

        new Thread(() -> {
            try {
                reportCheck.loadSourceFile(params.get("table").toString(), params.get("dataDt").toString(), params.get("newOldFlg").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
        return R.ok().put("loadSourceFileState", "starting");
    }


    @RequestMapping("/test")
    public R test(@RequestParam Map<String, Object> params) {
        LOG.debug("{}", "");
        LOG.debug("{}", reportCheck.getConfigByTable(params.get("table").toString()));
        LOG.debug("{}", reportCheck.getIndex(params.get("table").toString()));
        LOG.debug("{}", reportCheck.getIndexSeq(params.get("table").toString()));
        LOG.debug("{}", reportCheck.getTableInfoByTable(params.get("table").toString()));
        LOG.debug("{}", reportCheck.getIndex(params.get("table").toString()));
        LOG.debug("{}", reportCheck.getIndexSeq(params.get("table").toString()));
        LOG.debug("{}", reportCheck.getDescribe(params.get("table").toString()));
        return R.ok().put("out", "ok");
    }

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<LoadEntity> configList = loadService.queryList(query);
        int total = loadService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(configList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }
}
